import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class Compiler {
	// Global variable for the current token
	  public static String currentToken;
	  // Instance of the lexical analyzer
	  public static LexAnalyzer Lexer;
	  public static String docum = "";
	  
	  // It is assumed that the first argument provided is the name of the source file
	  // that is to be "compiled". 
	  public static void main(String[] args) throws FileNotFoundException, IOException, CompilerException {
	       // Initializations
	   Lexer = new LexAnalyzer();
	   SynAnalyzer Parser = new SynAnalyzer();
	   // instance of sem analyzer
	   SemAnalyzer sem = new SemAnalyzer();
	   
	   // Array for all test files
	   ArrayList<String> testCases = new ArrayList<String>();
	   testCases.add("Test1.txt");
	   testCases.add("Test2.mkd");
	   testCases.add("Test3.mkd");
	   testCases.add("Test4.mkd");
	   testCases.add("Test5.mkd");
	   testCases.add("Test6.mkd");
	   testCases.add("Test7.mkd");
	   testCases.add("Test8.mkd");
	   testCases.add("Test9.mkd");
	   testCases.add("Test10.mkd");
	   
	   
	    // variable for the file name
	    // Open the file - just change index number to test each test case
	     String fileName = "/Users/AshleyStrickler/Documents/senior - sem1/cosc455/eclipse/Proj1/src/proj1/Test2.mkd";
	     //Scanner input = new Scanner(new File(fileName));
	     //FileInputStream fstream = new FileInputStream(fileName);
	     //BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
	     //File f = new File(args[0]);
	     FileReader fr = new FileReader(fileName);
	     BufferedReader br = new BufferedReader(fr);
	     
	     // check test case extension for .mkd
	     String extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

	     if (!fileName.endsWith(".mkd")) { // not markdown file
	       System.out.println("ERROR: Choose a markdown file!");
	       System.exit(0);
	     }
	     // is markdown file
	     String strLine;
	     //Read File Line By Line & create string of entire file
	     while ((strLine = br.readLine()) != null)
	       docum += strLine;
	     
	    System.out.println(docum);
	      
	      // just a helper call to give the lex analyzer the string it needs to work with
	      //Lexer.start(docum);
	      // Get the first token
	      Lexer.getNextToken();
	     
	      // Parse the given sentence against the given grammar. 
	      Parser.document(); // this will make the entire parse tree - or print error & exit 
	   
	      // call sem analyzer for meaning & print doc
	   	  //sem.generateDoc(Parser.parseTree);
	      
	   	  // change file name from .mkd to .html
	      //String newFileName = fileName.replace("mkd", "html");
	      // write new html doc to new file
	      //FileWriter writer = new FileWriter(newFileName); 
	      //while(sem.newTree.size() != 0) {
	      //        writer.write(sem.newTree.remove(sem.newTree.size() - 1));
	      //}
	      //writer.close();
	    
	    // Close the file and clean up
	    br.close(); 
	   
	    } // close main

}
